<?php

namespace App\Strategies;

use App\Constants\CommentConstant;
use App\Helpers\DateUtils;
use App\Models\Factory\AuthFactory;
use App\Models\Factory\CommentFactory;
use App\Models\Factory\UserFactory;
use App\Services\Core\Store\Qiniu\QiniuService;
use App\Strategies\AppStrategy;
use Illuminate\Support\Facades\Auth;

/**
 * 评论公共策略
 *
 * @package App\Strategies
 */
class CommentStrategy extends AppStrategy
{
    /**
     * @param $productArr
     * 评论——产品标签+评论分数
     */
    public static function getCommentScore($productArr = [], $productId)
    {
        $datas['product_logo']          = QiniuService::getProductImgs($productArr['product_logo'], $productId);
        $datas['platform_product_name'] = isset($productArr['platform_product_name']) ? $productArr['platform_product_name'] : '';
        $datas['tag_name']              = isset($productArr['tag_name']) ? $productArr['tag_name'] : [];
        $rate                           = $productArr['composite_rate'];
        $speed                          = $productArr['loan_speed'];
        $exper                          = $productArr['experience'];
        $datas['composite_rate']        = isset($rate) ? number_format($rate, 1) : 0;
        $datas['loan_speed']            = isset($speed) ? number_format($speed, 1) : 0;
        $datas['experience']            = isset($exper) ? number_format($exper, 1) : 0;
        $datas['score']                 = bcdiv(bcadd($exper, bcadd($rate, $speed)), 3, 1);

        //所有评论个数
        $datas['count_all'] = CommentFactory::commentAllCount($productId);
        //有价值评论个数
        $datas['count_value'] = CommentFactory::commentValueCount($productId);

        return $datas ? $datas : [];
    }

    /**
     * @param $commentArr
     * @param $pageSize
     * @param $pageNum
     * 评论——有价值的评论数据
     */
    public static function getCommentValueLists($commentArr = [])
    {
        foreach ($commentArr as $k => $v) {
            $datas[$k]['platform_comment_id'] = $v['platform_comment_id'];
            $datas[$k]['content']             = $v['content'];
            $datas[$k]['score']               = CommentStrategy::getResultScore($v);
            $datas[$k]['result']              = CommentStrategy::resultIntTostr($v['result']);
            $datas[$k]['use_count']           = bcadd($v['use_count'], $v['add_count']);
            $datas[$k]['create_date']         = DateUtils::formatDate($v['create_date']);
            $datas[$k]['username']            = UserFactory::fetchUserName($v['create_id']);
        }

        return $datas ? $datas : [];
    }

    /**
     * @param $comment
     * @param $product
     * 评论——评论内容
     */
    public static function getCommentDatas($comment, $product)
    {
        $comment['content']               = !empty($comment['content']) ? $comment['content'] : '';
        $comment['loan_money']            = !empty($comment['loan_money']) ? $comment['loan_money'] : '';
        $comment['speed']                 = !empty($comment['speed']) ? $comment['speed'] : 0;
        $comment['rate']                  = !empty($comment['rate']) ? $comment['rate'] : 0;
        $comment['experience']            = !empty($comment['experience']) ? $comment['experience'] : 0;
        $comment['result']                = !empty($comment['result']) ? intval($comment['result']) : 0;
        $comment['product_logo']          = QiniuService::getProductImgs($product['product_logo'], $product['platform_product_id']);
        $comment['platform_product_name'] = !empty($product['platform_product_name']) ? $product['platform_product_name'] : '';
        $comment['platform_id']           = !empty($product['platform_id']) ? $product['platform_id'] : '';
        $comment['result_list']           = CommentStrategy::getResultLists();
        return $comment ? $comment : [];
    }

    /**
     * @param $str
     * @return bool
     * 评论——敏感词过滤
     */
    public static function sensitiveWordFilter($str)
    {
        $sensitive = CommentConstant::SENSITIVE;
        $blacklist = "/" . implode("|", $sensitive) . "/i";
        $bool      = preg_match($blacklist, $str, $matches);
        if (preg_match($blacklist, $str, $matches)) {
            return $matches[0];
        } else {
            return false;
        }
    }

    /**
     * @param array $data
     * 评论——修改评论内容
     */
    public static function updateCommentDatas($data = [])
    {
        $result = intval($data['resultId']);
        if ($result == 1 || $result == 5) {
            $data['speed']      = !empty($data['speed']) ? $data['speed'] : 0;
            $data['rate']       = !empty($data['rate']) ? $data['rate'] : 0;
            $data['experience'] = !empty($data['experience']) ? $data['experience'] : 0;
            $data['loan_money'] = !empty($data['loanMoney']) ? $data['loanMoney'] : 0;
        } else {
            $data['speed']      = 0;
            $data['rate']       = 0;
            $data['experience'] = !empty($data['experience']) ? $data['experience'] : 0;
            $data['loan_money'] = 0;
        }
        $data['platform_id'] = intval($data['platformId']);
        $data['content']     = !empty($data['content']) ? $data['content'] : '这个平台不错，利率很低，而且我很快就获得了借款';
        $data['result']      = !empty($data['resultId']) ? intval($data['resultId']) : 1;

        return $data ? $data : [];
    }


    ///////////////////////////////////////////////////////////////////////////////////
    /**
     * @param $data
     * @return mixed
     * 获取评论分数加成值
     */
    public static function getResultScore($data)
    {
        $i = $data['result'];
        if ($i == 1 || $i == 5) {
            return bcdiv(bcadd($data['experience'], bcadd($data['rate'], $data['speed'])), 3, 1);
        } else {
            return number_format($data['experience'], 1);
        }
    }

    /**
     * @param $data
     * @return string
     */
    public static function resultIntTostr($data)
    {
        $i = DateUtils::toInt($data);
        if ($i == 1) return CommentConstant::RESULT_ONE;
        elseif ($i == 2) return CommentConstant::RESULT_TWO;
        elseif ($i == 3) return CommentConstant::RESULT_THREE;
        elseif ($i == 4) return CommentConstant::RESULT_FOUR;
        elseif ($i == 5) return CommentConstant::RESULT_FIVE;
        else return CommentConstant::RESULT_FIVE;

    }

    /**
     * 评论——添加评论申请状态列表
     * result 1，钱到手 2，未通过 3 其他 4 申请中 ， 5 已获批
     * result_sign 代表星星行数 0一行 1三行
     */
    public static function getResultLists()
    {
        $resultLists = [
            [
                'result_id'   => 4,
                'result_name' => CommentConstant::RESULT_FOUR,
                'result_sign' => 0,
            ],
            [
                'result_id'   => 5,
                'result_name' => CommentConstant::RESULT_FIVE,
                'result_sign' => 1,
            ],
            [
                'result_id'   => 1,
                'result_name' => CommentConstant::RESULT_ONE,
                'result_sign' => 1,
            ],
            [
                'result_id'   => 2,
                'result_name' => CommentConstant::RESULT_TWO,
                'result_sign' => 0,
            ],
            [
                'result_id'   => 3,
                'result_name' => CommentConstant::RESULT_THREE,
                'result_sign' => 0,
            ],

        ];
        return $resultLists ? $resultLists : [];
    }
}
